import { RoleInterface } from '@/api/interface/auth/role';
import { PublicParams } from '@/api/interface/publicParams';
import { CustomTableInterface } from '@/components/ProTable/interface';
import { PublicInterface } from '@/interface/public';
import { useAddForm } from './use-addForm';
// 权限菜单
let permissionMenu = ref({
  visible: false,
  roleId: null
});

export function useDataGrid(proTableBox: PublicInterface.ProTableValue) {
  const { addForm, saveAddForm } = useAddForm(proTableBox);

  let dataGrid = reactive<CustomTableInterface>({
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'right', width: 320 },
      {
        prop: 'name',
        label: '角色名称'
      },
      { prop: 'remark', label: 'remark' }
    ],
    headButtonList: [
      {
        label: 'add',
        type: 'primary',
        // permissionCode: 'p:auth:role:save',
        icon: 'el-Plus',
        methods: () => {
          formOpen<RoleInterface.ReqSave>(addForm, 'NewRole');
        }
      }
    ],
    rowButtonList: [
      {
        label: 'edit',
        // permissionCode: 'p:auth:role:save',
        type: 'primary',
        display: row => row.status === BasicKey.ENABLE && row.name !== BasicKey.SUPER_ADMIN,
        methods: (row: RoleInterface.RoleInfo) => {
          formOpen<RoleInterface.ReqSave>(addForm, '编辑角色', row);
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'permissionSetting',
        type: 'primary',
        // permissionCode: 'p:auth:role:allocation',
        display: row => row.status === BasicKey.ENABLE && row.name !== BasicKey.SUPER_ADMIN,
        methods: (row: RoleInterface.RoleInfo) => {
          Object.assign(permissionMenu.value, {
            roleId: row.id,
            visible: true
          });
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'delete',
        type: 'danger',
        // permissionCode: 'p:auth:role:remove',
        display: row => row.status === BasicKey.ENABLE && row.name !== BasicKey.SUPER_ADMIN,
        methods: (row: RoleInterface.RoleInfo) => {
          const params = { id: row.id };
          const name = `删除【${row.name}】角色`;
          useHandleData(roleDeleteById, params, name, proTableBox.value?.getTableList);
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  // 对表格返回数据信息处理
  const tableDataCallback = (data: PublicParams.ResPage<RoleInterface.RoleInfo>) => {
    return data;
  };
  return {
    dataGrid,
    permissionMenu,
    tableDataCallback,
    addForm,
    saveAddForm
  };
}
